package com.aaa.car.service.impl;

import com.aaa.car.entity.MaintainShop;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.car.mapper.MaintainShopMapper;
import com.aaa.car.service.MaintainShopService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (MaintainShop)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-25 19:16:31
 */
@Service("maintainShopService")
public class MaintainShopServiceImpl implements MaintainShopService {
    @Resource
    private MaintainShopMapper maintainShopMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MaintainShop queryById(Integer id) {
        return this.maintainShopMapper.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo       查询当前页
     * @param pageSize     查询每页条数
     * @param maintainShop 对象实体
     * @return pageInfo 带分页列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, MaintainShop maintainShop) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<MaintainShop> pageInfo = new PageInfo<>(this.maintainShopMapper.queryAll(maintainShop));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return pageInfo 带分页列表
     */
    @Override
    public Result queryAll() {
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.maintainShopMapper.queryAll(null));
    }

    /**
     * 新增数据
     *
     * @param maintainShop 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainShop insert(MaintainShop maintainShop) {
        this.maintainShopMapper.insert(maintainShop);
        return maintainShop;
    }

    /**
     * 修改数据
     *
     * @param maintainShop 实例对象
     * @return 实例对象
     */
    @Override
    public MaintainShop update(MaintainShop maintainShop) {
        this.maintainShopMapper.update(maintainShop);
        return this.queryById(maintainShop.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.maintainShopMapper.deleteById(id) > 0;
    }
}